<script setup lang="ts" name="Operlog">
import detailPage from "./detail.vue";
import { cleanOperlogApi, delOperlogApi, listOperlogApi } from "@/api/monitor/operlog";

const { proxy } = getCurrentInstance();

const { sys_oper_type, sys_common_status } = useDict("sys_oper_type", "sys_common_status");

// 遮罩层
const loading = ref(false);

// 导出遮罩层
const exportLoading = ref(false);

// 搜索栏元素
const queryOperlogRef = ref(null);

// 表单Id
const operlog = ref<OperlogRecord>(null);

// 选中数组
const selection = ref<OperlogRecord[]>([]);

// 非单个禁用
const single = ref(true);

// 非多个禁用
const multiple = ref(true);

// 操作日志表数据
const operlogList = ref<OperlogRecord[]>([]);

// 弹出层标题
const title = ref("");

// 详情弹出层
const openDetail = ref(false);

//总条数
const total = ref(0);

// 列信息
const columns = ref<TableColumnsRecord>({
  colNo: { label: "序号", visible: true },
  operId: { label: "日志编号", visible: true },
  title: { label: "系统模块", visible: true },
  businessType: { label: "操作类型", visible: true },
  operName: { label: "操作人员", visible: true },
  operIp: { label: "主机", visible: true },
  operName: { label: "主机名称", visible: true },
  operLocation: { label: "主机网络类型", visible: true },
  operUrl: { label: "请求地址", visible: true },
  requestMethod: { label: "请求方式", visible: true },
  method: { label: "操作方法", visible: true },
  operParam: { label: "请求参数", visible: true },
  jsonResult: { label: "返回参数", visible: true },
  status: { label: "操作状态", visible: true },
  operTime: { label: "操作日期", visible: true },
  costTime: { label: "消耗时间", visible: true },
  errorMsg: { label: "异常信息", visible: true },
});

// 创建时间选择
const dateRange = ref<[string, string]>(['', '']);

// 查询参数
const queryParams = ref<OperlogRecord>({
  pageNum: 1,
  pageSize: 10,
  operId: null,
  title: null,
  businessType: null,
  operName: null,
  operIp: null,
  operName: null,
  operLocation: null,
  operUrl: null,
  requestMethod: null,
  method: null,
  operParam: null,
  jsonResult: null,
  status: null,
  operTime: null,
  costTime: null,
  errorMsg: null,
});

/** 查询操作日志列表 */
function getList() {
  loading.value = true;
  listOperlogApi(addDateRange(queryParams.value, dateRange.value)).then(response => {
    operlogList.value = response.rows;
    total.value = response.total;
    loading.value = false;
    operlogList.value.forEach((item, index) => {
      item['colNo'] = sequenceNo(index, queryParams.value);
    });
  });
}

/** 关闭弹窗 */
function closeDialog(refresh: boolean) {
  if (refresh) {
    getList();
  }
  openDetail.value = false;
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryOperlogRef.value.resetFields();
  dateRange.value = ['', ''];
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selectionList: OperlogRecord[]) {
  selection.value = selectionList;
  single.value = selectionList.length !== 1;
  multiple.value = !selectionList.length;
}


/** 详情按钮操作 */
function handleDetails(row: OperlogRecord) {
  operlog.value = row;
  openDetail.value = true;
  title.value = "操作日志详情";
}

/** 删除按钮操作 */
function handleDelete(row: OperlogRecord | any) {
  let ids = [];
  let colNos = [];
  if (row.operId) {
    ids = [row.operId];
    colNos = [row.colNo];
  } else {
    selection.value.forEach(item => {
      ids.push(item.operId);
      colNos.push(item.colNo);
    })
  }

  // @ts-ignore
  proxy.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项?', "系统提示", {
    type: "warning",
  }).then(() => {
    return delOperlogApi(ids);
  }).then(() => {
    getList();
    // @ts-ignore
    proxy.$modal.msgSuccess("删除成功");
  }).catch(err => {
    console.error(err);
  });
}

/** 清空按钮操作 */
function handleClean() {
  proxy.$modal.confirm("是否确认清空所有操作日志数据项?").then(function () {
    return cleanOperlogApi();
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("清空成功");
  }).catch(err => {
    console.error(err);
  });
}

/** 导出操作按钮 */
function handleExport() {
  download('monitor/operlog/export', {
    ...queryParams.value
  }, `操作日志_${new Date().getTime()}.xlsx`);
}

getList();
</script>

<template>
  <div class="app-container">
    <div class="back-item">
      <el-form :model="queryParams" ref="queryOperlogRef" :inline="true" label-width="68px">
        <el-form-item label="系统模块" prop="title">
          <el-input
              v-model="queryParams.title"
              placeholder="请输入系统模块"
              clearable
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="操作类型" prop="businessType">
          <el-select
              v-model="queryParams.businessType"
              placeholder="请选择操作类型"
              clearable
              @keyup.enter.native="handleQuery"
          >
            <el-option
                v-for="(item, index) in sys_oper_type"
                :key="index"
                :label="item.label"
                :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="操作人员" prop="operName">
          <el-input
              v-model="queryParams.operName"
              placeholder="请输入操作人员"
              clearable
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="操作状态" prop="status">
          <el-select
              v-model="queryParams.status"
              placeholder="请选择操作状态"
              clearable
              @keyup.enter.native="handleQuery"
          >
            <el-option
                v-for="(item, index) in sys_common_status"
                :key="index"
                :label="item.label"
                :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="操作日期" prop="operTime">
          <el-date-picker
              v-model="dateRange"
              value-format="YYYY-MM-DD"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item class="item-search">
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button type="warning" icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="back-item">
      <div class="table-top mb8">
        <div class="title title-left">
          操作日志列表
        </div>
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-has-permi="['monitor:operlog:remove']"
        >删除
        </el-button>
        <el-button
            type="danger"
            plain
            icon="Delete"
            @click="handleClean"
            v-hasPermi="['monitor:operlog:remove']"
        >清空
        </el-button>
        <el-button
            type="warning"
            plain
            icon="Download"
            :loading="exportLoading"
            @click="handleExport"
            v-has-permi="['monitor:operlog:export']"
        >导出
        </el-button>
        <span class="table-top-line" />
        <right-toolbar @queryTable="getList" :columns="columns" />
      </div>
      <el-table
          v-loading="loading"
          :data="operlogList"
          stripe
          v-tablesticky
          @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" width="55" prop="colNo" v-if="columns.colNo.visible" />
        <el-table-column label="日志编号" prop="operId" align="center" v-if="columns.operId.visible" />
        <el-table-column label="系统模块" prop="title" align="center" v-if="columns.title.visible" />
        <el-table-column label="操作类型" prop="businessType" align="center" v-if="columns.businessType.visible">
          <template #default="scope">
            <dict-tag :options="sys_oper_type" :value="scope.row.businessType" />
          </template>
        </el-table-column>
        <el-table-column label="操作人员" prop="operName" align="center" v-if="columns.operName.visible" />
        <el-table-column label="主机" prop="operIp" align="center" min-width="120" v-if="columns.operIp.visible" />
        <el-table-column label="请求地址" prop="operUrl" align="center" min-width="170" show-overflow-tooltip
                         v-if="columns.operUrl.visible" />
        <el-table-column label="请求方式" prop="requestMethod" align="center" v-if="columns.requestMethod.visible" />
        <el-table-column label="操作状态" prop="status" align="center" v-if="columns.status.visible">
          <template #default="scope">
            <dict-tag :options="sys_common_status" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="操作日期" prop="operTime" align="center" min-width="160"
                         v-if="columns.operTime.visible" />
        <el-table-column label="消耗时间" prop="costTime" align="center" v-if="columns.costTime.visible" />
        <el-table-column label="操作" align="center" fixed="right" min-width="120">
          <template #default="scope">
            <el-tooltip content="详情" placement="top">
              <el-button
                  link
                  type="primary"
                  icon="Document"
                  @click="handleDetails(scope.row)"
                  v-has-permi="['monitor:operlog:query']"
              />
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button
                  link
                  type="primary"
                  icon="Delete"
                  @click="handleDelete(scope.row)"
                  class="text-danger"
                  v-has-permi="['monitor:operlog:remove']"
              />
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <pagination
          :total="total"
          open-fixed
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      />
      <div class="line-magin" />
    </div>

    <!-- 添加或修改操作日志对话框 -->
    <detail-page v-if="openDetail" :row="operlog" :title="title" @closeDialog="closeDialog" />
  </div>
</template>

<style lang="scss" scoped>

</style>